package com.bosssoft.trainee.nontax.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.trainee.nontax.entity.dto.RoleDTO;
import com.bosssoft.trainee.nontax.entity.dto.RolePermissionDTO;
import com.bosssoft.trainee.nontax.entity.po.RolePO;
import com.bosssoft.trainee.nontax.entity.vo.RolePermissionVO;
import com.bosssoft.trainee.nontax.service.PermissionService;
import com.bosssoft.trainee.nontax.service.RoleService;
import com.bosssoft.trainee.nontax.utils.Result;
import com.bosssoft.trainee.nontax.entity.vo.query.RoleQueryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sct
 * @since 2022-06-15
 */
@RestController
@RequestMapping("/api/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @Resource
    private PermissionService permissionService;

    /**
     * 分页查询角色列表
     * @param roleQueryVO
     * @return
     */
    @GetMapping("/list")
    public Result<Object> list(RoleQueryVO roleQueryVO){
        //创建分页对象
        IPage<RolePO> page = new Page<>(roleQueryVO.getPageNo(),roleQueryVO.getPageSize());
        RoleDTO roleDTO = new RoleDTO();
        BeanUtils.copyProperties(roleQueryVO,roleDTO);
        roleService.findRoleListByUserId(page,roleDTO);
        return Result.ok(page);
    }

    /**
     *
     * 添加角色
     * @param roleQueryVO
     * @return
     */
    @PostMapping("/add")
    public Result<Object> add(@RequestBody RoleQueryVO roleQueryVO){
        RolePO rolePO = new RolePO();
        BeanUtils.copyProperties(roleQueryVO,rolePO);
        if(roleService.save(rolePO)){
            return Result.ok().message("角色添加成功");
        }
        return Result.error().message("角色添加失败");
    }

    /**
     * 修改角色
     * @param roleQueryVO
     * @return
     */
    @PutMapping("/update")
    public Result<Object> update(@RequestBody RoleQueryVO roleQueryVO){

        RolePO rolePO = new RolePO();
        BeanUtils.copyProperties(roleQueryVO,rolePO);
        if(roleService.updateById(rolePO)){
            return Result.ok().message("角色修改成功");
        }
        return Result.error().message("角色修改失败");
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result<Object> delete(@PathVariable Long id) {
        if (roleService.deleteRoleById(id)) {
            return Result.ok().message("角色删除成功");
        }
        return Result.error().message("角色删除失败");
    }

    /**
     * 检查用户角色是否被使用
     * @param id
     * @return
     */
    @GetMapping("/check/{id}")
    public Result<Object> check(@PathVariable Long id){
        if(roleService.hasRoleCount(id)){
            return Result.exist().message("该角色已经分配给其他用户，无法删除");
        }
        return Result.ok();
    }



    @PostMapping("/saveRoleAssign")
    public Result<Object> saveRoleAssign(@RequestBody RolePermissionDTO rolePermissionDTO) {
        if (roleService.saveRolePermission(rolePermissionDTO.getRoleId(),rolePermissionDTO.getList())) {
            return Result.ok().message("权限分配成功");
        }else {
            return Result.error().message("权限分配失败");
        }
    }

    @GetMapping("/getAssignPermissionTree")
    public Result getAssignPermissionTree(Long userId, Long roleId) {
        RolePermissionVO permissionTree = permissionService.findPermissionTree(userId, roleId);
        return Result.ok(permissionTree);
    }
}

